package wnhz.com.business.groupshop.adapter;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wnhz.com.business.R;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.base.RecyclerViewHolder;
import wnhz.com.business.groupshop.bean.CollectBean;
import wnhz.com.business.impl.ITaskFinishListener;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;

/**
 * Created by Administrator on 2017/6/29 0029.
 */

public class CollectAdapter extends BaseRecyclerAdapter<CollectBean.InfoBean.GoodinfoBean> {
    static final String TAG = "CollectAdapter";

    private MyItemClickLinstener ClickLinstener;
    private ProgressDialog mProgressDialog;
    private Context context;
    private List<CollectBean.InfoBean.GoodinfoBean> infoList;

    public CollectAdapter(Context context, List<CollectBean.InfoBean.GoodinfoBean> datas) {
        super(context, datas, R.layout.item_collect);
        this.context = context;
    }

    public void setOnClickListener(MyItemClickLinstener listener) {
        ClickLinstener = listener;
    }

    @Override
    protected void bindViewHolder(int position, RecyclerViewHolder holder) {
        ImageView iv_img = holder.getView(R.id.iv_img, false);
        TextView tv_title = holder.getView(R.id.tv_title, false);
        TextView tv_price = holder.getView(R.id.tv_price, false);
        TextView tv_cancle = holder.getView(R.id.tv_cancle, false);

        LogUtils.e("---------------","---------------"+getItem(position));

        CollectBean.InfoBean.GoodinfoBean item = getItem(position);

        tv_title.setText(item.getName());
        tv_price.setText("¥ " + item.getPrice());
        Glide.with(context).load(item.getImg()).into(iv_img);

        tv_cancle.setTag(R.id.tv_cancle, position);
        tv_cancle.setOnClickListener(itemClickLinstener);
    }

    @Override
    protected void loadData(int page, final ITaskFinishListener iTaskFinishListener) {
        SharedPreferences sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        String token = sp.getString("token", "");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);

        XUtil.Post(Url.Ucenter_myCollecttion, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("==我的收藏==", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    if (re.equals("1")){
                        Gson gson = new Gson();
                        CollectBean collectBean = gson.fromJson(result, CollectBean.class);
                        infoList = collectBean.getInfo().getGoodinfo();
                        iTaskFinishListener.onSuccess(TAG, infoList);
                    }else if(re.equals("0")){
                        iTaskFinishListener.onSuccess(TAG, infoList);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });

    }

    @Override
    protected void setPageSize(int pageSize) {

    }

    private  View.OnClickListener itemClickLinstener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (ClickLinstener != null) {
                int position;
                switch (v.getId()) {
                    case R.id.tv_cancle:
                        position = (int) v.getTag(R.id.tv_cancle);
                        LogUtils.e(TAG,"---------------------------------" + position);
                        ClickLinstener.OnCancleClickListener(CollectAdapter.this, position, v);
                        break;
                }
            }
        }
    };


    public interface MyItemClickLinstener {
        void OnCancleClickListener(CollectAdapter adapter, int position, View view);
    }
}
